package demo.jdbc;


import java.sql.*;

/**
 * @Auther liuteng on 2017/2/5.
 */
public class DataBaseDemo {
    public static void main(String[] args) throws SQLException, ClassNotFoundException {
//        1.注册驱动
        Class.forName("com.mysql.jdbc.Driver");
//        2.获取连接
        Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/testdb", "test", "test");
//        3.创建语句
        Statement statement = connection.createStatement();
//        4.执行语句
        ResultSet resultSet = statement.executeQuery("SELECT * FROM USER ");
//        5.获取结果
        while (resultSet.next()) {
            int column = resultSet.getMetaData().getColumnCount();
            for (int i = 1; i <= column; i++) {
                System.out.print(resultSet.getObject(i) + ((i == column) ? "\n" : ","));
            }
        }
//        6.关闭连接
        resultSet.close();
        statement.close();
        connection.close();
    }
}
